from person import db
from flask_security import RoleMixin, UserMixin
from werkzeug.security import generate_password_hash, check_password_hash  # 生成密码散列值函数


# 创建模型对象
class Department(db.Model):
    departmentID = db.Column(db.Integer, primary_key=True, autoincrement=True)
    departmentname = db.Column(db.String(100), nullable=False, unique=True)
    departmenttype = db.Column(db.Enum('部门', '公司'), server_default='部门', nullable=False)
    departmentphone = db.Column(db.BigInteger, nullable=False, unique=True)
    departmentfax = db.Column(db.BigInteger, nullable=True)
    describe = db.Column(db.Text, nullable=True)
    superior = db.Column(db.String(100), nullable=True)
    establish = db.Column(db.Date, nullable=False)


class Post(db.Model):
    postID = db.Column(db.Integer, primary_key=True, autoincrement=True)
    postname = db.Column(db.String(100), nullable=False, unique=True)
    posttype = db.Column(db.Enum('市场', '营销', '技术', '管理'), server_default='技术', nullable=False)
    establishment = db.Column(db.Integer, nullable=True)


class Staff(db.Model):
    userID = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100), nullable=False)
    sex = db.Column(db.Enum('男', '女'), server_default='男', nullable=False)
    birthday = db.Column(db.Date, nullable=False)
    IDnumber = db.Column(db.String(18), nullable=False, unique=True)
    departmentID = db.Column(db.Integer, db.ForeignKey('department.departmentID'))  # 添加外键
    postID = db.Column(db.Integer, db.ForeignKey('post.postID'))
    hiredate = db.Column(db.Date, nullable=False)
    takejobdate = db.Column(db.Date, nullable=False)
    employment_form = db.Column(db.Enum('正式员工', '临时员工'), server_default='正式员工', nullable=False)
    personnel_source = db.Column(db.Enum('校园招聘', '社会招聘', '其他'), server_default='社会招聘', nullable=False)
    politics_status = db.Column(db.Enum('党员', '预备党员', '团员', '其他'))
    nation = db.Column(db.Enum('基诺族', '珞巴族', '门巴族', '赫哲族', '鄂伦春族', '独龙族', '塔塔尔族', '京族',
                               '裕固族', '保安族', '德昂族', '鄂温克族', '俄罗斯族', '乌孜别克族', '怒族',
                               '塔吉克族', '普米族', '阿昌族', '锡伯族', '仡佬族', '毛南族', '撒拉族',
                               '布朗族', '羌族', '仫佬族', '达斡尔族', '土族', '柯尔克孜族', '景颇族',
                               '纳西族', '东乡族', '水族', '拉祜族', '高山族', '畲族', '佤族', '僳僳族',
                               '黎族', '傣族', '哈萨克族', '哈尼族', '土家族', '白族', '瑶族', '侗族',
                               '满族', '朝鲜族', '布依族', '壮族', '彝族', '苗族', '维吾尔族', '藏族',
                               '回族', '蒙古族', '汉族'))
    native_place = db.Column(db.Text)
    phone = db.Column(db.BigInteger, nullable=False, unique=True)
    email = db.Column(db.String(50), nullable=False, unique=True)
    height = db.Column(db.Integer)
    bloodtype = db.Column(db.Enum('A型', 'B型', 'AB型', 'O型', '其他'))
    marriage = db.Column(db.Enum('未婚', '已婚', '丧偶', '离婚', '其他'))
    birthplace = db.Column(db.Text)
    domicileplace = db.Column(db.Text)
    highest_degree = db.Column(db.Enum('高中及以下', '大专', '本科', '研究生'))
    highest_offering = db.Column(db.Enum('无学位', '学士', '双学士', '硕士', '博士', '博士后'))
    graduate_school = db.Column(db.String(255))
    major = db.Column(db.String(255))
    graduation_date = db.Column(db.Date)
    now = db.Column(db.Boolean)

roles_users = db.Table('roles_users',
                       db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
                       db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

class User(db.Model, UserMixin):
    id = db.Column(db.Integer,primary_key=True)
    userID = db.Column(db.Integer, db.ForeignKey('staff.userID'),nullable=False)
    password = db.Column(db.String(128), nullable=False)
    phone = db.Column(db.BigInteger, nullable=False, unique=True)
    email = db.Column(db.String(50), nullable=False, unique=True)
    active = db.Column(db.Boolean())
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

    def set_password(password):  # 用来设置密码的方法，接受密码作为参数
        return generate_password_hash(password)  # 将生产的密码保持到对应的字段

    def validate_password(self, password):  # 用于验证密码的方法，接受密码作为参数
        return check_password_hash(self.password, password)
        # 返回布尔值


class Staff_probation(db.Model):
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    userID = db.Column(db.Integer, db.ForeignKey('staff.userID'),nullable=False)
    startdate = db.Column(db.Date, nullable=False)
    enddate = db.Column(db.Date, nullable=False)


class Probation(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    userID = db.Column(db.Integer, db.ForeignKey('staff.userID'), nullable=False)
    examine_remark = db.Column(db.Text, nullable=False)
    examine_result = db.Column(db.Enum('转正', '延期', '不予录用'), server_default='转正', nullable=False)
    disposedate = db.Column(db.Date, nullable=False)
    remark = db.Column(db.Text)


class Staff_dimission(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    userID = db.Column(db.Integer, db.ForeignKey('staff.userID'), nullable=False)
    termdate = db.Column(db.Date, nullable=False)
    dimission_type = db.Column(db.Enum('主动辞职', '辞退', '退休', '开除', '试用期未通过'), server_default='主动辞职', nullable=False)
    leave_to = db.Column(db.String(255))
    talent_pool = db.Column(db.Enum('是', '否'), server_default='是', nullable=False)
    remark = db.Column(db.Text)


class Staff_career(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    userID = db.Column(db.Integer, db.ForeignKey('staff.userID'), nullable=False)
    starttime = db.Column(db.Date, nullable=False)
    endtime = db.Column(db.Date, nullable=False)
    company_name = db.Column(db.String(255))
    job_content = db.Column(db.String(255))
    duty = db.Column(db.String(255))
    pay = db.Column(db.Float)
    certifier = db.Column(db.String(50))
    certifier_duty = db.Column(db.String(255))
    certifier_phone = db.Column(db.Integer)
    remark = db.Column(db.Text)


class Staff_languages(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    userID = db.Column(db.Integer, db.ForeignKey('staff.userID'), nullable=False)
    language = db.Column(db.String(100), nullable=False)
    prificiency = db.Column(db.Enum('完全不懂', '少量，不能进行业务沟通', '有限的业务沟通',
                                    '一般，业务沟通仍受少量限制', '好，无困难地进行谈判和演讲',
                                    '流利，在商务中自如地运用'))


class Staff_relation(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    userID = db.Column(db.Integer, db.ForeignKey('staff.userID'),nullable=False)
    relation = db.Column(db.Enum('父亲', '母亲', '配偶'), server_default='父亲', nullable=False)
    name = db.Column(db.String(50), nullable=False)
    company = db.Column(db.String(255))
    position = db.Column(db.String(255))
    phone = db.Column(db.Integer, unique=True)


class Nation(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    nation = db.Column(db.String(20),nullable=False)

# db.create_all()